package com.nit.lab.vote.controller.vote;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nit.lab.vote.dto.system.RoleDTO;
import com.nit.lab.vote.dto.system.TokenDTO;
import com.nit.lab.vote.dto.utils.TableReqDTO;
import com.nit.lab.vote.dto.utils.TableRspDTO;
import com.nit.lab.vote.dto.vote.CPDTO;
import com.nit.lab.vote.dto.vote.CandidateDTO;
import com.nit.lab.vote.dto.utils.OpResultDTO;
import com.nit.lab.vote.service.vote.CandidateService;
import com.nit.lab.vote.utils.HttpRequestReader;
import com.nit.lab.vote.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping(value = "/api/candidate")
public class CandidateController {
    @Resource
    private CandidateService candidateService;
    private final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    /**
     * @Description: 查询候选人
     * @Date: 2020/12/10
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO query( @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = candidateService.list4table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 添加候选人
     * @Date: 2020/12/10
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody CandidateDTO candidateDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            candidateService.add(candidateDTO);
        } catch (Exception e) {
            op.setMessage("error");
            op.setResult("导入失败");
        }
        return op;
    }

    /**
     * @Description: 修改候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody CandidateDTO candidateDTO) {
        OpResultDTO op;
        try {
            op = candidateService.update(candidateDTO);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("修改失败");
        }
        return op;
    }


    /**
     * @Description: 删除候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(@RequestBody CandidateDTO candidateDTO) {
        OpResultDTO op;
        try {
            op = candidateService.remove(candidateDTO);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("删除失败");
        }
        return op;
    }



    /**
     * @Description: 根据projectId查询候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/queryByProject", method = RequestMethod.POST)
    public TableRspDTO queryByProject( @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = candidateService.queryByProject(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 编辑候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/queryAllProject", method = RequestMethod.POST)
    public TableRspDTO queryAllProject( @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = candidateService.queryAllProject(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 为项目添加候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/addByProject", method = RequestMethod.POST)
    public OpResultDTO addByProject(@RequestBody CPDTO cpDTO) {
        OpResultDTO op;
        try {
            op = candidateService.addByProject(cpDTO);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("添加失败");
        }
        return op;
    }

    /**
     * @Description: 为项目删除候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/removeByProject", method = RequestMethod.POST)
    public OpResultDTO removeByProject(@RequestBody CPDTO cpDTO) {
        OpResultDTO op;
        try {
            op = candidateService.removeByProject(cpDTO);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("添加失败");
        }
        return op;
    }



    /**
     * @Description: 查询项目候选人的附件
     * @Date: 2020/12/15
     */
    @RequestMapping(value = "/queryFile", method = RequestMethod.POST)
    public OpResultDTO queryFile(@RequestBody CPDTO cpDTO) {
        OpResultDTO op = null;
        try {
            op = candidateService.queryFile(cpDTO);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("添加失败");
        }
        return op;
    }

    /**
     * @Description: 为项目候选人上传附件
     * @Date: 2020/12/13
     */
    @RequestMapping(value = "/addFile", method = RequestMethod.POST)
    public OpResultDTO addFile(HttpServletRequest request) {
        OpResultDTO op = null;
        try {
            CPDTO cpDTO = JSON.parseObject(request.getParameter("data"), CPDTO.class);
            List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
            op = candidateService.addFile(cpDTO, files.get(0));
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("添加失败");
        }
        return op;
    }

    /**
     * @Description: 删除候选人附件
     * @Date: 2020/12/13
     */
    @RequestMapping(value = "/removeFile", method = RequestMethod.POST)
    public OpResultDTO removeFile(HttpServletRequest request) {
        OpResultDTO op = null;
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            op = candidateService.removeFile(jsonObject.getString("file"));
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("删除失败");
        }
        return op;
    }

}
